<template>
	<view class="page">

		<div class="avatarHead u-rela u-flex u-p-30"
			@click="$u.route({url:'/pagesB/setting/userInfo',params:{avatar:userInfo.headimgurl}})">
			<u-avatar :src="userInfo.headimgurl" class="u-m-r-20" size="large"></u-avatar>
			<div>
				<h3 class="title">{{userInfo.user_name}}</h3>
				<p class="phone">{{userInfo.phone|| ''}}</p>
			</div>
			<u-icon name="arrow-right" class="right" color="#999" size="28"></u-icon>
		</div>
		<u-cell-group>
			<u-cell-item :title="$t('收货地址管理')" @click="navigateTo('/pagesB/my/address')"></u-cell-item>
		</u-cell-group>

		<u-cell-group class="u-m-t-20">
			<u-cell-item :title="$t('修改手机号')" @click="navigateTo('/pagesB/setting/editPhone')"></u-cell-item>
			<u-cell-item :title="$t('修改登录密码')" @click="navigateTo('/pagesB/setting/modify')"></u-cell-item>
			<u-cell-item :title="$t('支付密码管理')" @click="navigateTo('/pagesB/setting/setPayPwd')"></u-cell-item>
			<!-- <u-cell-item  title="清除缓存"  @click="clearStorage()" :value="currentSize+'M'"></u-cell-item> -->
		</u-cell-group>

		<u-cell-group class="u-m-t-20">
			<!-- <u-cell-item title="功能反馈" @click="$u.route('/pagesB/my/feedback')"></u-cell-item> -->
			<u-cell-item :title="$t('关于我们')" @click="goAboutus"></u-cell-item>
			<!-- #ifdef APP-PLUS -->
			<u-cell-item :title="$t('当前版本')" @click="onAPPUpdate" :value="appVersion" :arrow="false"></u-cell-item>
			<!-- #endif -->
		</u-cell-group>
		<div class="logoutBtn" @click="cancelAccount(userInfo.id)">{{$t('账号注销')}}</div>
		<div class="logoutBtn" @click="logout">{{$t('退出登录')}}</div>
	</view>
</template>

<script>
	// #ifdef APP-PLUS
	import APPUpdate, {
		getCurrentNo
	} from '@/uni_modules/zhouWei-APPUpdate/js_sdk/appUpdate';
	// #endif
	import {
		SERVERTAPI
	} from '@/config/index'
	const webSocket = require("../../utils/socket.js");
	export default {
		data() {
			return {
				currentSize: 0, //当前缓存大小
				Server: '',
				userInfo: {},
				appVersion: '',
			};
		},

		computed: {
			// getuserInfo(){
			//   return this.$store.getters.getuserInfo
			// }
		},
		onLoad(option) {
			// #ifdef APP-PLUS
			getCurrentNo(res => {
				// 进页面获取当前APP版本号（用于页面显示）
				this.appVersion = "V" + res.versionName;
			});
			// #endif
			uni.setNavigationBarTitle({
				title: this.$t('设置')
			});

		},
		onShow() {
			this.getStorageInfoSync();
			this.getUserInfo();
		},
		methods: {
			// 检查APP是否有新版本
			onAPPUpdate() {
				// true 没有新版本的时候有提示，默认：false
				APPUpdate(true);
			},
			navigateTo(url) {
				this.$navigateTo(url);
			},
			getUserInfo() {
				this.$http.getUserInfo().then(res => {
					if (res.status == 200) {
						this.userInfo = res.data;
					}
				})
			},
			// 退出登陆
			logout() {
				$utils.showModal({
					content: this.$t('确认退出账户吗') + '?'
				}).then(res => {

					if (res.confirm) {
						uni.closeSocket({
							success() {
								console.log('socket关闭成功');
							},
							fail(res) {
								console.log(res);
							}
						})
						uni.clearStorageSync();
						uni.setStorageSync('baseUrl', SERVERTAPI); // baseUrl需要保持缓存
						// uni.removeStorageSync('coverImg');
						// uni.removeStorageSync('token');
						// uni.removeStorageSync('device_token');
						this.$toast(this.$t('退出登录成功'));

						this.$u.route({
							url: "/pages/tabBar/my",
							type: "reLaunch"
						})
					}
				})

			},
			//注销账号
			cancelAccount(e){
				$utils.showModal({
					content: this.$t('确认注销账户吗')+'?'
				}).then(res => {
					if (res.confirm) {
						const data = {
							id: e,
						};
						this.$http.cancelAccount(data).then(res => {
							if (res.status == 200) {
								uni.closeSocket({
									success() {
										console.log('socket关闭成功');
									},
									fail(res) {
										console.log(res);
									}
								})
								uni.clearStorageSync();
								uni.setStorageSync('baseUrl',SERVERTAPI); // baseUrl需要保持缓存
								// uni.removeStorageSync('coverImg');
								// uni.removeStorageSync('token');
								// uni.removeStorageSync('device_token');
								this.$toast(this.$t('退出登录成功'));
								
								this.$u.route({
									url: "/pages/tabBar/my",
									type: "reLaunch"
								})
							}
						});
					}
				});
			},
			// 获取缓存大小
			getStorageInfoSync() {
				uni.getStorageInfo({
					success: res => {
						this.currentSize = (res.currentSize / 1024).toFixed(2);
					}
				});
			},
			goAboutus() {
				this.$u.route({
					url: '/pagesB/article/detail',
					params: {
						title: this.$t('关于我们')
					}
				})
			},
			// 清除缓存
			clearStorage() {
				$utils.showModal({
					content: this.$t('确定要清除缓存吗') + '?'
				}).then(res => {
					if (res.confirm) {
						try {
							uni.clearStorageSync();
							this.currentSize = 0
						} catch (e) {
							// error
						}
					}
				})

			}
		}
	};
</script>

<style lang="scss" scoped>
	@import "~uview-ui/index.scss";

	.logoutBtn {
		padding: 20upx;
		background-color: #fff;
		margin-top: 40upx;
		text-align: center;
		color: #666;
	}

	.title {
		font-size: 32upx;
		margin-bottom: 10upx;
	}

	.phone {
		font-size: 24upx;
		color: #999;
	}

	.avatarHead {
		background-color: #fff;
		border-bottom: 1px solid #f5f5f5;

		.right {
			position: absolute;
			right: 30upx;
			top: 50%;
			transform: translateY(-50%);
		}
	}
</style>
